home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / perl5 / URI.pm < prev   
Text File  |  2008-06-16  |  31KB  |  1,040 lines

  1. package URI;
  2.  
  3. use strict;
  4. use vars qw($VERSION);
  5. $VERSION = "1.37";
  6.  
  7. use vars qw($ABS_REMOTE_LEADING_DOTS $ABS_ALLOW_RELATIVE_SCHEME $DEFAULT_QUERY_FORM_DELIMITER);
  8.  
  9. my %implements;  # mapping from scheme to implementor class
  10.  
  11. # Some "official" character classes
  12.  
  13. use vars qw($reserved $mark $unreserved $uric $scheme_re);
  14. $reserved   = q(;/?:@&=+$,[]);
  15. $mark       = q(-_.!~*'());                                    #'; emacs
  16. $unreserved = "A-Za-z0-9\Q$mark\E";
  17. $uric       = quotemeta($reserved) . $unreserved . "%";
  18.  
  19. $scheme_re  = '[a-zA-Z][a-zA-Z0-9.+\-]*';
  20.  
  21. use Carp ();
  22. use URI::Escape ();
  23.  
  24. use overload ('""'     => sub { ${$_[0]} },
  25.           '=='     => sub { overload::StrVal($_[0]) eq
  26.                                 overload::StrVal($_[1])
  27.                               },
  28.               fallback => 1,
  29.              );
  30.  
  31. sub new
  32. {
  33.     my($class, $uri, $scheme) = @_;
  34.  
  35.     $uri = defined ($uri) ? "$uri" : "";   # stringify
  36.     # Get rid of potential wrapping
  37.     $uri =~ s/^<(?:URL:)?(.*)>$/$1/;  # 
  38.     $uri =~ s/^"(.*)"$/$1/;
  39.     $uri =~ s/^\s+//;
  40.     $uri =~ s/\s+$//;
  41.  
  42.     my $impclass;
  43.     if ($uri =~ m/^($scheme_re):/so) {
  44.     $scheme = $1;
  45.     }
  46.     else {
  47.     if (($impclass = ref($scheme))) {
  48.         $scheme = $scheme->scheme;
  49.     }
  50.     elsif ($scheme && $scheme =~ m/^($scheme_re)(?::|$)/o) {
  51.         $scheme = $1;
  52.         }
  53.     }
  54.     $impclass ||= implementor($scheme) ||
  55.     do {
  56.         require URI::_foreign;
  57.         $impclass = 'URI::_foreign';
  58.     };
  59.  
  60.     return $impclass->_init($uri, $scheme);
  61. }
  62.  
  63.  
  64. sub new_abs
  65. {
  66.     my($class, $uri, $base) = @_;
  67.     $uri = $class->new($uri, $base);
  68.     $uri->abs($base);
  69. }
  70.  
  71.  
  72. sub _init
  73. {
  74.     my $class = shift;
  75.     my($str, $scheme) = @_;
  76.     # find all funny characters and encode the bytes.
  77.     $str =~ s*([^$uric\#])* URI::Escape::escape_char($1) *ego;
  78.     $str = "$scheme:$str" unless $str =~ /^$scheme_re:/o ||
  79.                                  $class->_no_scheme_ok;
  80.     my $self = bless \$str, $class;
  81.     $self;
  82. }
  83.  
  84.  
  85. sub implementor
  86. {
  87.     my($scheme, $impclass) = @_;
  88.     if (!$scheme || $scheme !~ /\A$scheme_re\z/o) {
  89.     require URI::_generic;
  90.     return "URI::_generic";
  91.     }
  92.  
  93.     $scheme = lc($scheme);
  94.  
  95.     if ($impclass) {
  96.     # Set the implementor class for a given scheme
  97.         my $old = $implements{$scheme};
  98.         $impclass->_init_implementor($scheme);
  99.         $implements{$scheme} = $impclass;
  100.         return $old;
  101.     }
  102.  
  103.     my $ic = $implements{$scheme};
  104.     return $ic if $ic;
  105.  
  106.     # scheme not yet known, look for internal or
  107.     # preloaded (with 'use') implementation
  108.     $ic = "URI::$scheme";  # default location
  109.  
  110.     # turn scheme into a valid perl identifier by a simple tranformation...
  111.     $ic =~ s/\+/_P/g;
  112.     $ic =~ s/\./_O/g;
  113.     $ic =~ s/\-/_/g;
  114.  
  115.     no strict 'refs';
  116.     # check we actually have one for the scheme:
  117.     unless (@{"${ic}::ISA"}) {
  118.         # Try to load it
  119.         eval "require $ic";
  120.         die $@ if $@ && $@ !~ /Can\'t locate.*in \@INC/;
  121.         return unless @{"${ic}::ISA"};
  122.     }
  123.  
  124.     $ic->_init_implementor($scheme);
  125.     $implements{$scheme} = $ic;
  126.     $ic;
  127. }
  128.  
  129.  
  130. sub _init_implementor
  131. {
  132.     my($class, $scheme) = @_;
  133.     # Remember that one implementor class may actually
  134.     # serve to implement several URI schemes.
  135. }
  136.  
  137.  
  138. sub clone
  139. {
  140.     my $self = shift;
  141.     my $other = $$self;
  142.     bless \$other, ref $self;
  143. }
  144.  
  145.  
  146. sub _no_scheme_ok { 0 }
  147.  
  148. sub _scheme
  149. {
  150.     my $self = shift;
  151.  
  152.     unless (@_) {
  153.     return unless $$self =~ /^($scheme_re):/o;
  154.     return $1;
  155.     }
  156.  
  157.     my $old;
  158.     my $new = shift;
  159.     if (defined($new) && length($new)) {
  160.     Carp::croak("Bad scheme '$new'") unless $new =~ /^$scheme_re$/o;
  161.     $old = $1 if $$self =~ s/^($scheme_re)://o;
  162.     my $newself = URI->new("$new:$$self");
  163.     $$self = $$newself; 
  164.     bless $self, ref($newself);
  165.     }
  166.     else {
  167.     if ($self->_no_scheme_ok) {
  168.         $old = $1 if $$self =~ s/^($scheme_re)://o;
  169.         Carp::carp("Oops, opaque part now look like scheme")
  170.         if $^W && $$self =~ m/^$scheme_re:/o
  171.     }
  172.     else {
  173.         $old = $1 if $$self =~ m/^($scheme_re):/o;
  174.     }
  175.     }
  176.  
  177.     return $old;
  178. }
  179.  
  180. sub scheme
  181. {
  182.     my $scheme = shift->_scheme(@_);
  183.     return unless defined $scheme;
  184.     lc($scheme);
  185. }
  186.  
  187.  
  188. sub opaque
  189. {
  190.     my $self = shift;
  191.  
  192.     unless (@_) {
  193.     $$self =~ /^(?:$scheme_re:)?([^\#]*)/o or die;
  194.     return $1;
  195.     }
  196.  
  197.     $$self =~ /^($scheme_re:)?    # optional scheme
  198.             ([^\#]*)          # opaque
  199.                 (\#.*)?           # optional fragment
  200.               $/sx or die;
  201.  
  202.     my $old_scheme = $1;
  203.     my $old_opaque = $2;
  204.     my $old_frag   = $3;
  205.  
  206.     my $new_opaque = shift;
  207.     $new_opaque = "" unless defined $new_opaque;
  208.     $new_opaque =~ s/([^$uric])/ URI::Escape::escape_char($1)/ego;
  209.  
  210.     $$self = defined($old_scheme) ? $old_scheme : "";
  211.     $$self .= $new_opaque;
  212.     $$self .= $old_frag if defined $old_frag;
  213.  
  214.     $old_opaque;
  215. }
  216.  
  217. *path = \&opaque;  # alias
  218.  
  219.  
  220. sub fragment
  221. {
  222.     my $self = shift;
  223.     unless (@_) {
  224.     return unless $$self =~ /\#(.*)/s;
  225.     return $1;
  226.     }
  227.  
  228.     my $old;
  229.     $old = $1 if $$self =~ s/\#(.*)//s;
  230.  
  231.     my $new_frag = shift;
  232.     if (defined $new_frag) {
  233.     $new_frag =~ s/([^$uric])/ URI::Escape::escape_char($1) /ego;
  234.     $$self .= "#$new_frag";
  235.     }
  236.     $old;
  237. }
  238.  
  239.  
  240. sub as_string
  241. {
  242.     my $self = shift;
  243.     $$self;
  244. }
  245.  
  246.  
  247. sub canonical
  248. {
  249.     # Make sure scheme is lowercased, that we don't escape unreserved chars,
  250.     # and that we use upcase escape sequences.
  251.  
  252.     my $self = shift;
  253.     my $scheme = $self->_scheme || "";
  254.     my $uc_scheme = $scheme =~ /[A-Z]/;
  255.     my $esc = $$self =~ /%[a-fA-F0-9]{2}/;
  256.     return $self unless $uc_scheme || $esc;
  257.  
  258.     my $other = $self->clone;
  259.     if ($uc_scheme) {
  260.     $other->_scheme(lc $scheme);
  261.     }
  262.     if ($esc) {
  263.     $$other =~ s{%([0-9a-fA-F]{2})}
  264.                 { my $a = chr(hex($1));
  265.                       $a =~ /^[$unreserved]\z/o ? $a : "%\U$1"
  266.                     }ge;
  267.     }
  268.     return $other;
  269. }
  270.  
  271. # Compare two URIs, subclasses will provide a more correct implementation
  272. sub eq {
  273.     my($self, $other) = @_;
  274.     $self  = URI->new($self, $other) unless ref $self;
  275.     $other = URI->new($other, $self) unless ref $other;
  276.     ref($self) eq ref($other) &&                # same class
  277.     $self->canonical->as_string eq $other->canonical->as_string;
  278. }
  279.  
  280. # generic-URI transformation methods
  281. sub abs { $_[0]; }
  282. sub rel { $_[0]; }
  283.  
  284. # help out Storable
  285. sub STORABLE_freeze {
  286.        my($self, $cloning) = @_;
  287.        return $$self;
  288. }
  289.  
  290. sub STORABLE_thaw {
  291.        my($self, $cloning, $str) = @_;
  292.        $$self = $str;
  293. }
  294.  
  295. 1;
  296.  
  297. __END__
  298.  
  299. =head1 NAME
  300.  
  301. URI - Uniform Resource Identifiers (absolute and relative)
  302.  
  303. =head1 SYNOPSIS
  304.  
  305.  $u1 = URI->new("http://www.perl.com");
  306.  $u2 = URI->new("foo", "http");
  307.  $u3 = $u2->abs($u1);
  308.  $u4 = $u3->clone;
  309.  $u5 = URI->new("HTTP://WWW.perl.com:80")->canonical;
  310.  
  311.  $str = $u->as_string;
  312.  $str = "$u";
  313.  
  314.  $scheme = $u->scheme;
  315.  $opaque = $u->opaque;
  316.  $path   = $u->path;
  317.  $frag   = $u->fragment;
  318.  
  319.  $u->scheme("ftp");
  320.  $u->host("ftp.perl.com");
  321.  $u->path("cpan/");
  322.  
  323. =head1 DESCRIPTION
  324.  
  325. This module implements the C<URI> class.  Objects of this class
  326. represent "Uniform Resource Identifier references" as specified in RFC
  327. 2396 (and updated by RFC 2732).
  328.  
  329. A Uniform Resource Identifier is a compact string of characters that
  330. identifies an abstract or physical resource.  A Uniform Resource
  331. Identifier can be further classified as either a Uniform Resource Locator
  332. (URL) or a Uniform Resource Name (URN).  The distinction between URL
  333. and URN does not matter to the C<URI> class interface. A
  334. "URI-reference" is a URI that may have additional information attached
  335. in the form of a fragment identifier.
  336.  
  337. An absolute URI reference consists of three parts:  a I<scheme>, a
  338. I<scheme-specific part> and a I<fragment> identifier.  A subset of URI
  339. references share a common syntax for hierarchical namespaces.  For
  340. these, the scheme-specific part is further broken down into
  341. I<authority>, I<path> and I<query> components.  These URIs can also
  342. take the form of relative URI references, where the scheme (and
  343. usually also the authority) component is missing, but implied by the
  344. context of the URI reference.  The three forms of URI reference
  345. syntax are summarized as follows:
  346.  
  347.   <scheme>:<scheme-specific-part>#<fragment>
  348.   <scheme>://<authority><path>?<query>#<fragment>
  349.   <path>?<query>#<fragment>
  350.  
  351. The components into which a URI reference can be divided depend on the
  352. I<scheme>.  The C<URI> class provides methods to get and set the
  353. individual components.  The methods available for a specific
  354. C<URI> object depend on the scheme.
  355.  
  356. =head1 CONSTRUCTORS
  357.  
  358. The following methods construct new C<URI> objects:
  359.  
  360. =over 4
  361.  
  362. =item $uri = URI->new( $str )
  363.  
  364. =item $uri = URI->new( $str, $scheme )
  365.  
  366. Constructs a new URI object.  The string
  367. representation of a URI is given as argument, together with an optional
  368. scheme specification.  Common URI wrappers like "" and <>, as well as
  369. leading and trailing white space, are automatically removed from
  370. the $str argument before it is processed further.
  371.  
  372. The constructor determines the scheme, maps this to an appropriate
  373. URI subclass, constructs a new object of that class and returns it.
  374.  
  375. The $scheme argument is only used when $str is a
  376. relative URI.  It can be either a simple string that
  377. denotes the scheme, a string containing an absolute URI reference, or
  378. an absolute C<URI> object.  If no $scheme is specified for a relative
  379. URI $str, then $str is simply treated as a generic URI (no scheme-specific
  380. methods available).
  381.  
  382. The set of characters available for building URI references is
  383. restricted (see L<URI::Escape>).  Characters outside this set are
  384. automatically escaped by the URI constructor.
  385.  
  386. =item $uri = URI->new_abs( $str, $base_uri )
  387.  
  388. Constructs a new absolute URI object.  The $str argument can
  389. denote a relative or absolute URI.  If relative, then it is
  390. absolutized using $base_uri as base. The $base_uri must be an absolute
  391. URI.
  392.  
  393. =item $uri = URI::file->new( $filename )
  394.  
  395. =item $uri = URI::file->new( $filename, $os )
  396.  
  397. Constructs a new I<file> URI from a file name.  See L<URI::file>.
  398.  
  399. =item $uri = URI::file->new_abs( $filename )
  400.  
  401. =item $uri = URI::file->new_abs( $filename, $os )
  402.  
  403. Constructs a new absolute I<file> URI from a file name.  See
  404. L<URI::file>.
  405.  
  406. =item $uri = URI::file->cwd
  407.  
  408. Returns the current working directory as a I<file> URI.  See
  409. L<URI::file>.
  410.  
  411. =item $uri->clone
  412.  
  413. Returns a copy of the $uri.
  414.  
  415. =back
  416.  
  417. =head1 COMMON METHODS
  418.  
  419. The methods described in this section are available for all C<URI>
  420. objects.
  421.  
  422. Methods that give access to components of a URI always return the
  423. old value of the component.  The value returned is C<undef> if the
  424. component was not present.  There is generally a difference between a
  425. component that is empty (represented as C<"">) and a component that is
  426. missing (represented as C<undef>).  If an accessor method is given an
  427. argument, it updates the corresponding component in addition to
  428. returning the old value of the component.  Passing an undefined
  429. argument removes the component (if possible).  The description of
  430. each accessor method indicates whether the component is passed as
  431. an escaped or an unescaped string.  A component that can be further
  432. divided into sub-parts are usually passed escaped, as unescaping might
  433. change its semantics.
  434.  
  435. The common methods available for all URI are:
  436.  
  437. =over 4
  438.  
  439. =item $uri->scheme
  440.  
  441. =item $uri->scheme( $new_scheme )
  442.  
  443. Sets and returns the scheme part of the $uri.  If the $uri is
  444. relative, then $uri->scheme returns C<undef>.  If called with an
  445. argument, it updates the scheme of $uri, possibly changing the
  446. class of $uri, and returns the old scheme value.  The method croaks
  447. if the new scheme name is illegal; a scheme name must begin with a
  448. letter and must consist of only US-ASCII letters, numbers, and a few
  449. special marks: ".", "+", "-".  This restriction effectively means
  450. that the scheme must be passed unescaped.  Passing an undefined
  451. argument to the scheme method makes the URI relative (if possible).
  452.  
  453. Letter case does not matter for scheme names.  The string
  454. returned by $uri->scheme is always lowercase.  If you want the scheme
  455. just as it was written in the URI in its original case,
  456. you can use the $uri->_scheme method instead.
  457.  
  458. =item $uri->opaque
  459.  
  460. =item $uri->opaque( $new_opaque )
  461.  
  462. Sets and returns the scheme-specific part of the $uri
  463. (everything between the scheme and the fragment)
  464. as an escaped string.
  465.  
  466. =item $uri->path
  467.  
  468. =item $uri->path( $new_path )
  469.  
  470. Sets and returns the same value as $uri->opaque unless the URI
  471. supports the generic syntax for hierarchical namespaces.
  472. In that case the generic method is overridden to set and return
  473. the part of the URI between the I<host name> and the I<fragment>.
  474.  
  475. =item $uri->fragment
  476.  
  477. =item $uri->fragment( $new_frag )
  478.  
  479. Returns the fragment identifier of a URI reference
  480. as an escaped string.
  481.  
  482. =item $uri->as_string
  483.  
  484. Returns a URI object to a plain string.  URI objects are
  485. also converted to plain strings automatically by overloading.  This
  486. means that $uri objects can be used as plain strings in most Perl
  487. constructs.
  488.  
  489. =item $uri->canonical
  490.  
  491. Returns a normalized version of the URI.  The rules
  492. for normalization are scheme-dependent.  They usually involve
  493. lowercasing the scheme and Internet host name components,
  494. removing the explicit port specification if it matches the default port,
  495. uppercasing all escape sequences, and unescaping octets that can be
  496. better represented as plain characters.
  497.  
  498. For efficiency reasons, if the $uri is already in normalized form,
  499. then a reference to it is returned instead of a copy.
  500.  
  501. =item $uri->eq( $other_uri )
  502.  
  503. =item URI::eq( $first_uri, $other_uri )
  504.  
  505. Tests whether two URI references are equal.  URI references
  506. that normalize to the same string are considered equal.  The method
  507. can also be used as a plain function which can also test two string
  508. arguments.
  509.  
  510. If you need to test whether two C<URI> object references denote the
  511. same object, use the '==' operator.
  512.  
  513. =item $uri->abs( $base_uri )
  514.  
  515. Returns an absolute URI reference.  If $uri is already
  516. absolute, then a reference to it is simply returned.  If the $uri
  517. is relative, then a new absolute URI is constructed by combining the
  518. $uri and the $base_uri, and returned.
  519.  
  520. =item $uri->rel( $base_uri )
  521.  
  522. Returns a relative URI reference if it is possible to
  523. make one that denotes the same resource relative to $base_uri.
  524. If not, then $uri is simply returned.
  525.  
  526. =back
  527.  
  528. =head1 GENERIC METHODS
  529.  
  530. The following methods are available to schemes that use the
  531. common/generic syntax for hierarchical namespaces.  The descriptions of
  532. schemes below indicate which these are.  Unknown schemes are
  533. assumed to support the generic syntax, and therefore the following
  534. methods:
  535.  
  536. =over 4
  537.  
  538. =item $uri->authority
  539.  
  540. =item $uri->authority( $new_authority )
  541.  
  542. Sets and returns the escaped authority component
  543. of the $uri.
  544.  
  545. =item $uri->path
  546.  
  547. =item $uri->path( $new_path )
  548.  
  549. Sets and returns the escaped path component of
  550. the $uri (the part between the host name and the query or fragment).
  551. The path can never be undefined, but it can be the empty string.
  552.  
  553. =item $uri->path_query
  554.  
  555. =item $uri->path_query( $new_path_query )
  556.  
  557. Sets and returns the escaped path and query
  558. components as a single entity.  The path and the query are
  559. separated by a "?" character, but the query can itself contain "?".
  560.  
  561. =item $uri->path_segments
  562.  
  563. =item $uri->path_segments( $segment, ... )
  564.  
  565. Sets and returns the path.  In a scalar context, it returns
  566. the same value as $uri->path.  In a list context, it returns the
  567. unescaped path segments that make up the path.  Path segments that
  568. have parameters are returned as an anonymous array.  The first element
  569. is the unescaped path segment proper;  subsequent elements are escaped
  570. parameter strings.  Such an anonymous array uses overloading so it can
  571. be treated as a string too, but this string does not include the
  572. parameters.
  573.  
  574. Note that absolute paths have the empty string as their first
  575. I<path_segment>, i.e. the I<path> C</foo/bar> have 3
  576. I<path_segments>; "", "foo" and "bar".
  577.  
  578. =item $uri->query
  579.  
  580. =item $uri->query( $new_query )
  581.  
  582. Sets and returns the escaped query component of
  583. the $uri.
  584.  
  585. =item $uri->query_form
  586.  
  587. =item $uri->query_form( $key1 => $val1, $key2 => $val2, ... )
  588.  
  589. =item $uri->query_form( $key1 => $val1, $key2 => $val2, ..., $delim )
  590.  
  591. =item $uri->query_form( \@key_value_pairs )
  592.  
  593. =item $uri->query_form( \@key_value_pairs, $delim )
  594.  
  595. =item $uri->query_form( \%hash )
  596.  
  597. =item $uri->query_form( \%hash, $delim )
  598.  
  599. Sets and returns query components that use the
  600. I<application/x-www-form-urlencoded> format.  Key/value pairs are
  601. separated by "&", and the key is separated from the value by a "="
  602. character.
  603.  
  604. The form can be set either by passing separate key/value pairs, or via
  605. an array or hash reference.  Passing an empty array or an empty hash
  606. removes the query component, whereas passing no arguments at all leaves
  607. the component unchanged.  The order of keys is undefined if a hash
  608. reference is passed.  The old value is always returned as a list of
  609. separate key/value pairs.  Assigning this list to a hash is unwise as
  610. the keys returned might repeat.
  611.  
  612. The values passed when setting the form can be plain strings or
  613. references to arrays of strings.  Passing an array of values has the
  614. same effect as passing the key repeatedly with one value at a time.
  615. All the following statements have the same effect:
  616.  
  617.     $uri->query_form(foo => 1, foo => 2);
  618.     $uri->query_form(foo => [1, 2]);
  619.     $uri->query_form([ foo => 1, foo => 2 ]);
  620.     $uri->query_form([ foo => [1, 2] ]);
  621.     $uri->query_form({ foo => [1, 2] });
  622.  
  623. The $delim parameter can be passed as ";" to force the key/value pairs
  624. to be delimited by ";" instead of "&" in the query string.  This
  625. practice is often recommened for URLs embedded in HTML or XML
  626. documents as this avoids the trouble of escaping the "&" character.
  627. You might also set the $URI::DEFAULT_QUERY_FORM_DELIMITER variable to
  628. ";" for the same global effect.
  629.  
  630. The C<URI::QueryParam> module can be loaded to add further methods to
  631. manipulate the form of a URI.  See L<URI::QueryParam> for details.
  632.  
  633. =item $uri->query_keywords
  634.  
  635. =item $uri->query_keywords( $keywords, ... )
  636.  
  637. =item $uri->query_keywords( \@keywords )
  638.  
  639. Sets and returns query components that use the
  640. keywords separated by "+" format.
  641.  
  642. The keywords can be set either by passing separate keywords directly
  643. or by passing a reference to an array of keywords.  Passing an empty
  644. array removes the query component, whereas passing no arguments at
  645. all leaves the component unchanged.  The old value is always returned
  646. as a list of separate words.
  647.  
  648. =back
  649.  
  650. =head1 SERVER METHODS
  651.  
  652. For schemes where the I<authority> component denotes an Internet host,
  653. the following methods are available in addition to the generic
  654. methods.
  655.  
  656. =over 4
  657.  
  658. =item $uri->userinfo
  659.  
  660. =item $uri->userinfo( $new_userinfo )
  661.  
  662. Sets and returns the escaped userinfo part of the
  663. authority component.
  664.  
  665. For some schemes this is a user name and a password separated by
  666. a colon.  This practice is not recommended. Embedding passwords in
  667. clear text (such as URI) has proven to be a security risk in almost
  668. every case where it has been used.
  669.  
  670. =item $uri->host
  671.  
  672. =item $uri->host( $new_host )
  673.  
  674. Sets and returns the unescaped hostname.
  675.  
  676. If the $new_host string ends with a colon and a number, then this
  677. number also sets the port.
  678.  
  679. =item $uri->port
  680.  
  681. =item $uri->port( $new_port )
  682.  
  683. Sets and returns the port.  The port is a simple integer
  684. that should be greater than 0.
  685.  
  686. If a port is not specified explicitly in the URI, then the URI scheme's default port
  687. is returned. If you don't want the default port
  688. substituted, then you can use the $uri->_port method instead.
  689.  
  690. =item $uri->host_port
  691.  
  692. =item $uri->host_port( $new_host_port )
  693.  
  694. Sets and returns the host and port as a single
  695. unit.  The returned value includes a port, even if it matches the
  696. default port.  The host part and the port part are separated by a
  697. colon: ":".
  698.  
  699. =item $uri->default_port
  700.  
  701. Returns the default port of the URI scheme to which $uri
  702. belongs.  For I<http> this is the number 80, for I<ftp> this
  703. is the number 21, etc.  The default port for a scheme can not be
  704. changed.
  705.  
  706. =back
  707.  
  708. =head1 SCHEME-SPECIFIC SUPPORT
  709.  
  710. Scheme-specific support is provided for the following URI schemes.  For C<URI>
  711. objects that do not belong to one of these, you can only use the common and
  712. generic methods.
  713.  
  714. =over 4
  715.  
  716. =item B<data>:
  717.  
  718. The I<data> URI scheme is specified in RFC 2397.  It allows inclusion
  719. of small data items as "immediate" data, as if it had been included
  720. externally.
  721.  
  722. C<URI> objects belonging to the data scheme support the common methods
  723. and two new methods to access their scheme-specific components:
  724. $uri->media_type and $uri->data.  See L<URI::data> for details.
  725.  
  726. =item B<file>:
  727.  
  728. An old specification of the I<file> URI scheme is found in RFC 1738.
  729. A new RFC 2396 based specification in not available yet, but file URI
  730. references are in common use.
  731.  
  732. C<URI> objects belonging to the file scheme support the common and
  733. generic methods.  In addition, they provide two methods for mapping file URIs
  734. back to local file names; $uri->file and $uri->dir.  See L<URI::file>
  735. for details.
  736.  
  737. =item B<ftp>:
  738.  
  739. An old specification of the I<ftp> URI scheme is found in RFC 1738.  A
  740. new RFC 2396 based specification in not available yet, but ftp URI
  741. references are in common use.
  742.  
  743. C<URI> objects belonging to the ftp scheme support the common,
  744. generic and server methods.  In addition, they provide two methods for
  745. accessing the userinfo sub-components: $uri->user and $uri->password.
  746.  
  747. =item B<gopher>:
  748.  
  749. The I<gopher> URI scheme is specified in
  750. <draft-murali-url-gopher-1996-12-04> and will hopefully be available
  751. as a RFC 2396 based specification.
  752.  
  753. C<URI> objects belonging to the gopher scheme support the common,
  754. generic and server methods. In addition, they support some methods for
  755. accessing gopher-specific path components: $uri->gopher_type,
  756. $uri->selector, $uri->search, $uri->string.
  757.  
  758. =item B<http>:
  759.  
  760. The I<http> URI scheme is specified in RFC 2616.
  761. The scheme is used to reference resources hosted by HTTP servers.
  762.  
  763. C<URI> objects belonging to the http scheme support the common,
  764. generic and server methods.
  765.  
  766. =item B<https>:
  767.  
  768. The I<https> URI scheme is a Netscape invention which is commonly
  769. implemented.  The scheme is used to reference HTTP servers through SSL
  770. connections.  Its syntax is the same as http, but the default
  771. port is different.
  772.  
  773. =item B<ldap>:
  774.  
  775. The I<ldap> URI scheme is specified in RFC 2255.  LDAP is the
  776. Lightweight Directory Access Protocol.  An ldap URI describes an LDAP
  777. search operation to perform to retrieve information from an LDAP
  778. directory.
  779.  
  780. C<URI> objects belonging to the ldap scheme support the common,
  781. generic and server methods as well as ldap-specific methods: $uri->dn,
  782. $uri->attributes, $uri->scope, $uri->filter, $uri->extensions.  See
  783. L<URI::ldap> for details.
  784.  
  785. =item B<ldapi>:
  786.  
  787. Like the I<ldap> URI scheme, but uses a UNIX domain socket.  The
  788. server methods are not supported, and the local socket path is
  789. available as $uri->un_path.  The I<ldapi> scheme is used by the
  790. OpenLDAP package.  There is no real specification for it, but it is
  791. mentioned in various OpenLDAP manual pages.
  792.  
  793. =item B<ldaps>:
  794.  
  795. Like the I<ldap> URI scheme, but uses an SSL connection.  This
  796. scheme is deprecated, as the preferred way is to use the I<start_tls>
  797. mechanism.
  798.  
  799. =item B<mailto>:
  800.  
  801. The I<mailto> URI scheme is specified in RFC 2368.  The scheme was
  802. originally used to designate the Internet mailing address of an
  803. individual or service.  It has (in RFC 2368) been extended to allow
  804. setting of other mail header fields and the message body.
  805.  
  806. C<URI> objects belonging to the mailto scheme support the common
  807. methods and the generic query methods.  In addition, they support the
  808. following mailto-specific methods: $uri->to, $uri->headers.
  809.  
  810. =item B<mms>:
  811.  
  812. The I<mms> URL specification can be found at L<http://sdp.ppona.com/>
  813. C<URI> objects belonging to the mms scheme support the common,
  814. generic, and server methods, with the exception of userinfo and
  815. query-related sub-components.
  816.  
  817. =item B<news>:
  818.  
  819. The I<news>, I<nntp> and I<snews> URI schemes are specified in
  820. <draft-gilman-news-url-01> and will hopefully be available as an RFC
  821. 2396 based specification soon.
  822.  
  823. C<URI> objects belonging to the news scheme support the common,
  824. generic and server methods.  In addition, they provide some methods to
  825. access the path: $uri->group and $uri->message.
  826.  
  827. =item B<nntp>:
  828.  
  829. See I<news> scheme.
  830.  
  831. =item B<pop>:
  832.  
  833. The I<pop> URI scheme is specified in RFC 2384. The scheme is used to
  834. reference a POP3 mailbox.
  835.  
  836. C<URI> objects belonging to the pop scheme support the common, generic
  837. and server methods.  In addition, they provide two methods to access the
  838. userinfo components: $uri->user and $uri->auth
  839.  
  840. =item B<rlogin>:
  841.  
  842. An old specification of the I<rlogin> URI scheme is found in RFC
  843. 1738. C<URI> objects belonging to the rlogin scheme support the
  844. common, generic and server methods.
  845.  
  846. =item B<rtsp>:
  847.  
  848. The I<rtsp> URL specification can be found in section 3.2 of RFC 2326.
  849. C<URI> objects belonging to the rtsp scheme support the common,
  850. generic, and server methods, with the exception of userinfo and
  851. query-related sub-components.
  852.  
  853. =item B<rtspu>:
  854.  
  855. The I<rtspu> URI scheme is used to talk to RTSP servers over UDP
  856. instead of TCP.  The syntax is the same as rtsp.
  857.  
  858. =item B<rsync>:
  859.  
  860. Information about rsync is available from http://rsync.samba.org.
  861. C<URI> objects belonging to the rsync scheme support the common,
  862. generic and server methods.  In addition, they provide methods to
  863. access the userinfo sub-components: $uri->user and $uri->password.
  864.  
  865. =item B<sip>:
  866.  
  867. The I<sip> URI specification is described in sections 19.1 and 25
  868. of RFC 3261.  C<URI> objects belonging to the sip scheme support the
  869. common, generic, and server methods with the exception of path related
  870. sub-components.  In addition, they provide two methods to get and set
  871. I<sip> parameters: $uri->params_form and $uri->params.
  872.  
  873. =item B<sips>:
  874.  
  875. See I<sip> scheme.  Its syntax is the same as sip, but the default
  876. port is different.
  877.  
  878. =item B<snews>:
  879.  
  880. See I<news> scheme.  Its syntax is the same as news, but the default
  881. port is different.
  882.  
  883. =item B<telnet>:
  884.  
  885. An old specification of the I<telnet> URI scheme is found in RFC
  886. 1738. C<URI> objects belonging to the telnet scheme support the
  887. common, generic and server methods.
  888.  
  889. =item B<tn3270>:
  890.  
  891. These URIs are used like I<telnet> URIs but for connections to IBM
  892. mainframes.  C<URI> objects belonging to the tn3270 scheme support the
  893. common, generic and server methods.
  894.  
  895. =item B<ssh>:
  896.  
  897. Information about ssh is available at http://www.openssh.com/.
  898. C<URI> objects belonging to the ssh scheme support the common,
  899. generic and server methods. In addition, they provide methods to
  900. access the userinfo sub-components: $uri->user and $uri->password.
  901.  
  902. =item B<urn>:
  903.  
  904. The syntax of Uniform Resource Names is specified in RFC 2141.  C<URI>
  905. objects belonging to the urn scheme provide the common methods, and also the
  906. methods $uri->nid and $uri->nss, which return the Namespace Identifier
  907. and the Namespace-Specific String respectively.
  908.  
  909. The Namespace Identifier basically works like the Scheme identifier of
  910. URIs, and further divides the URN namespace.  Namespace Identifier
  911. assignments are maintained at
  912. <http://www.iana.org/assignments/urn-namespaces>.
  913.  
  914. Letter case is not significant for the Namespace Identifier.  It is
  915. always returned in lower case by the $uri->nid method.  The $uri->_nid
  916. method can be used if you want it in its original case.
  917.  
  918. =item B<urn>:B<isbn>:
  919.  
  920. The C<urn:isbn:> namespace contains International Standard Book
  921. Numbers (ISBNs) and is described in RFC 3187.  A C<URI> object belonging
  922. to this namespace has the following extra methods (if the
  923. Business::ISBN module is available): $uri->isbn,
  924. $uri->isbn_publisher_code, $uri->isbn_group_code (formerly isbn_country_code,
  925. which is still supported by issues a deprecation warning), $uri->isbn_as_ean.
  926.  
  927. =item B<urn>:B<oid>:
  928.  
  929. The C<urn:oid:> namespace contains Object Identifiers (OIDs) and is
  930. described in RFC 3061.  An object identifier consists of sequences of digits
  931. separated by dots.  A C<URI> object belonging to this namespace has an
  932. additional method called $uri->oid that can be used to get/set the oid
  933. value.  In a list context, oid numbers are returned as separate elements.
  934.  
  935. =back
  936.  
  937. =head1 CONFIGURATION VARIABLES
  938.  
  939. The following configuration variables influence how the class and its
  940. methods behave:
  941.  
  942. =over 4
  943.  
  944. =item $URI::ABS_ALLOW_RELATIVE_SCHEME
  945.  
  946. Some older parsers used to allow the scheme name to be present in the
  947. relative URL if it was the same as the base URL scheme.  RFC 2396 says
  948. that this should be avoided, but you can enable this old behaviour by
  949. setting the $URI::ABS_ALLOW_RELATIVE_SCHEME variable to a TRUE value.
  950. The difference is demonstrated by the following examples:
  951.  
  952.   URI->new("http:foo")->abs("http://host/a/b")
  953.       ==>  "http:foo"
  954.  
  955.   local $URI::ABS_ALLOW_RELATIVE_SCHEME = 1;
  956.   URI->new("http:foo")->abs("http://host/a/b")
  957.       ==>  "http:/host/a/foo"
  958.  
  959.  
  960. =item $URI::ABS_REMOTE_LEADING_DOTS
  961.  
  962. You can also have the abs() method ignore excess ".."
  963. segments in the relative URI by setting $URI::ABS_REMOTE_LEADING_DOTS
  964. to a TRUE value.  The difference is demonstrated by the following
  965. examples:
  966.  
  967.   URI->new("../../../foo")->abs("http://host/a/b")
  968.       ==> "http://host/../../foo"
  969.  
  970.   local $URI::ABS_REMOTE_LEADING_DOTS = 1;
  971.   URI->new("../../../foo")->abs("http://host/a/b")
  972.       ==> "http://host/foo"
  973.  
  974. =item $URI::DEFAULT_QUERY_FORM_DELIMITER
  975.  
  976. This value can be set to ";" to have the query form C<key=value> pairs
  977. delimited by ";" instead of "&" which is the default.
  978.  
  979. =back
  980.  
  981. =head1 BUGS
  982.  
  983. Using regexp variables like $1 directly as arguments to the URI methods
  984. does not work too well with current perl implementations.  I would argue
  985. that this is actually a bug in perl.  The workaround is to quote
  986. them. Example:
  987.  
  988.    /(...)/ || die;
  989.    $u->query("$1");
  990.  
  991. =head1 PARSING URIs WITH REGEXP
  992.  
  993. As an alternative to this module, the following (official) regular
  994. expression can be used to decode a URI:
  995.  
  996.   my($scheme, $authority, $path, $query, $fragment) =
  997.   $uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:#(.*))?|;
  998.  
  999. The C<URI::Split> module provides the function uri_split() as a
  1000. readable alternative.
  1001.  
  1002. =head1 SEE ALSO
  1003.  
  1004. L<URI::file>, L<URI::WithBase>, L<URI::QueryParam>, L<URI::Escape>,
  1005. L<URI::Split>, L<URI::Heuristic>
  1006.  
  1007. RFC 2396: "Uniform Resource Identifiers (URI): Generic Syntax",
  1008. Berners-Lee, Fielding, Masinter, August 1998.
  1009.  
  1010. http://www.iana.org/assignments/uri-schemes
  1011.  
  1012. http://www.iana.org/assignments/urn-namespaces
  1013.  
  1014. http://www.w3.org/Addressing/
  1015.  
  1016. =head1 COPYRIGHT
  1017.  
  1018. Copyright 1995-2004,2008 Gisle Aas.
  1019.  
  1020. Copyright 1995 Martijn Koster.
  1021.  
  1022. This program is free software; you can redistribute it and/or modify
  1023. it under the same terms as Perl itself.
  1024.  
  1025. =head1 AUTHORS / ACKNOWLEDGMENTS
  1026.  
  1027. This module is based on the C<URI::URL> module, which in turn was
  1028. (distantly) based on the C<wwwurl.pl> code in the libwww-perl for
  1029. perl4 developed by Roy Fielding, as part of the Arcadia project at the
  1030. University of California, Irvine, with contributions from Brooks
  1031. Cutter.
  1032.  
  1033. C<URI::URL> was developed by Gisle Aas, Tim Bunce, Roy Fielding and
  1034. Martijn Koster with input from other people on the libwww-perl mailing
  1035. list.
  1036.  
  1037. C<URI> and related subclasses was developed by Gisle Aas.
  1038.  
  1039. =cut
  1040.